package com.treetao.union.web.controller.system;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treetao.union.db.model.SysRescource;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.web.constant.Constants;
import com.treetao.union.web.controller.BaseAdminController;

/**
 * 系统资源管理
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/system/rescource")
public class SysRescourceController extends BaseAdminController{

    @GetMapping("list")
    public String list(){
        return "admin/system/rescource/list";
    }

    //@RequiresPermissions("sys:rescource:list")
    @PostMapping("list")
    @ResponseBody
    public LayerData<SysRescource> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                     @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                     ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<SysRescource> layerData = new LayerData<>();
        QueryWrapper<SysRescource> wrapper = new QueryWrapper<>();
        if(!map.isEmpty()){
            String type = (String) map.get("type");
            if(StringUtils.isNotBlank(type) && "image".equals(type)) {
                wrapper.like("file_type", type);
            }
            if(StringUtils.isNotBlank(type) && "video".equals(type)) {
                wrapper.like("file_type", type);
            }
//            String hash = (String)map.get("hash");
//            if(StringUtils.isNotBlank(hash)){
//                wrapper.eq("hash",hash);
//            }
//            String source = (String)map.get("source");
//            if(StringUtils.isNotBlank(source)){
//                wrapper.eq("source",source);
//            }
        }
        IPage<SysRescource> dataPage = sysRescourceService.page(new Page<>(page,limit),wrapper);
        layerData.setCount(dataPage.getTotal());
        layerData.setData(dataPage.getRecords());
        return layerData;
    }

   // @RequiresPermissions("sys:rescource:delete")
    @PostMapping("delete")
    @ResponseBody
    public RestResponse delete(@RequestParam("id") Long id){
        if(id == null){
            return RestResponse.failure("删除ID不能为空");
        }
        QueryWrapper<SysRescource> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysRescource sysRes = sysRescourceService.getOne(wrapper);
        if(sysRes==null) {
        	return RestResponse.failure("删除失败");
        }
        
        boolean flag = sysRescourceService.remove(wrapper);
//        if(flag) {
//        	//从磁盘上删除文件
//        	LocalUploadService.delete(sysRes.getFileName());
//        }
        return RestResponse.success();
    }

    @GetMapping("test")
    public String error() throws Exception {
        throw new Exception("测试异常消息");
    }
}

    